﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HttpRequest;

namespace TestHttpRequest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ParseHttpRequest hp = new ParseHttpRequest();
            string url = "http://10.10.42.133:8080/wms/service?SERVICE=WMS&VERSION=1.1.1&LAYERS=basic&REQUEST=GetMap&STYLES=&SRS=EPSG%3A3785f&BBOX=116.306076,39.854938,116.462631,39.985012";
            string baseUrl = "http://10.10.42.133:8080/wms/service?SERVICE=WMS&VERSION=1.1.1&LAYERS=basic&REQUEST=GetMap&STYLES=&SRS=EPSG%3A3785f&BBOX=";
            string bboxStr = "116.28891,39.849667,116.465378,39.990273";
            IntPtr ip = hp.GetTile(baseUrl+bboxStr).GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ip,IntPtr.Zero,Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            hp.Dele(ip); 

            this.tile.Source = bitmapSource;
             
        }

        public BitmapSource GetBitMapSource(string Url)
        {
            string baseUrl = "http://10.10.42.133:8080/wms/service?SERVICE=WMS&VERSION=1.1.1&LAYERS=basic&REQUEST=GetMap&STYLES=&SRS=EPSG%3A3785f&BBOX=";
            
            ParseHttpRequest hp = new ParseHttpRequest();
            IntPtr ip = hp.GetTile(baseUrl + Url).GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            hp.Dele(ip);
            return bitmapSource;
        }

        public BitmapSource montageTiles(Point a1,Point b1)
        {
            int i = 0;
            for (i = 0; i < 19; i++)
            {
                if ((lngToPixel(b1.X, i) - lngToPixel(a1.X, i)) > this.tile.Width)
                {
                    if((latToPixel(a1.Y,i)-latToPixel(b1.Y ,i)) > this.tile.Height)
                    {
                        break;
                    }
                }
            }

            double width = lngToPixel(b1.X, i) - lngToPixel(a1.X,i);
            double height = latToPixel(a1.Y, i) - latToPixel(b1.Y, i);

            int nX = (int)(width / 256);
            int nY = (int)(height / 256);

            int minX = (int)lngToPixel(a1.X,i);
            int minY = (int)latToPixel(b1.Y,i);

            int num = GetTileNum(i);
            int j = 0,m=0;
            for (j = 0; j < num-1; j++)
            {
                int X0 = j * 256;
                int X0_Plus = X0 + 256;

                //double dX0 = lngToPixel(X0,i);
                if (X0 <= minX && minX <= X0_Plus)
                {
                    for (; m < num - 1; )
                    {
                        int Y0 = m *256;
                        int Y0_Plus = Y0 + 256;

                        //double dY0 = latToPixel(Y0,i);
                        if (Y0 <= minY && minY <= Y0_Plus)
                        {
                            break;
                        }
                    }
                }
            }
            int x = j * 256;
            int y = m * 256;
            BitmapSource[,] bs = new BitmapSource[nX,nY];

            for (int Xa = 0; Xa < nX; Xa++)
            {
                for (int Xb = 0; Xb < nY; Xb++)
                {
                    double X1 = pixelToLng(x, i);
                    double Y1 = pixelToLat(y + 256, i);
                    double X2 = pixelToLng(x+256,i);
                    double Y2 = pixelToLat(y,i);

                    string bboxStr = X1.ToString() + 
                        "," + Y1.ToString() + 
                        "," + X2.ToString() + 
                        "," + Y2.ToString();

                    bs[Xa, Xb] = GetBitMapSource(bboxStr);
                }
            }
            BitmapSource bitmapSource = null;
            return bitmapSource;
        }

        public int GetTileNum(int zoom)
        {
            int a = 1;
            do
            {
                if(zoom != 0)
                    a = a * 2;
            }
            while (zoom-- > 0);

            return a;
        }

        public double lngToPixel(double lng, int zoom)
        {
            double lon = lng + 180.0;
            lon = lon * (256L << zoom);
            lon = lon / 360.0;
            return lon;
        }

        public double latToPixel(double lat, int zoom)
        {
            double siny = Math.Sin(lat * 0.0174532925194444);
            double y = Math.Log((1 + siny) / (1 - siny));
            return (128 << zoom) * (1 - y / 6.283185307);
        }

        public double pixelToLat(double pixelY, int zoom)
        {
            double y = 6.283185307 * (1 - pixelY / (128 << zoom));
            double z = Math.Exp(y);
            double siny = (z - 1) / (z + 1);
            return Math.Asin(siny) * 57.295779514719953;
        }

        public double pixelToLng(double pixelX, int zoom)
        {
            double tmpPixelX = pixelX * 360.0;
            tmpPixelX = tmpPixelX / (256L << zoom);
            double result = tmpPixelX - 180.0;
            return result;
        } 

    }
}
